IH=1;
ONLT=0;
aJIME = new Array();
bJIME = new Array();
aKeys = new Array();
aActs = new Array();
aCoos = new Array();
jobj = 0;
jico = 0;
jinp = 0;
PGmax = 1;
PGcur = 1;
PGsiz = 20;
PGjmp = document.body;
jsr = document;
ABSC = 0;
NOMSGS = 0;
DOSK = 1;

dragstart = 0;
eventx = 0;
eventy = 0;
dragobj = 0;
IHOST = 'http://cospho.googlepages.com/';
AddJIME('xo','を');AddJIME('xts','っ');AddJIME('xwa','ゎ');
AddJIME('xya','ゃ');AddJIME('xyu','ゅ');AddJIME('xyo','ょ');
AddJIME('wa','わ');
AddJIME('xa','ぁ');AddJIME('xi','ぃ');AddJIME('xu','ぅ');AddJIME('xe','ぇ');AddJIME('xo','ぉ');
AddJIME('ka','か');AddJIME('ki','き');AddJIME('ku','く');AddJIME('ke','け');AddJIME('ko','こ');
AddJIME('ga','が');AddJIME('gi','ぎ');AddJIME('gu','ぐ');AddJIME('ge','げ');AddJIME('go','ご');
AddJIME('sa','さ');AddJIME('si','し');AddJIME('su','す');AddJIME('se','せ');AddJIME('so','そ');
AddJIME('za','ざ');AddJIME('zi','じ');AddJIME('zu','ず');AddJIME('ze','ぜ');AddJIME('zo','ぞ');
AddJIME('ta','た');AddJIME('ti','ち');AddJIME('ts','つ');AddJIME('te','て');AddJIME('to','と');
AddJIME('da','だ');AddJIME('zi','ぢ');AddJIME('zu','づ');AddJIME('de','で');AddJIME('do','ど');
AddJIME('na','な');AddJIME('ni','に');AddJIME('nu','ぬ');AddJIME('ne','ね');AddJIME('no','の');
AddJIME('ha','は');AddJIME('hi','ひ');AddJIME('hu','ふ');AddJIME('he','へ');AddJIME('ho','ほ');
AddJIME('ba','ば');AddJIME('bi','び');AddJIME('bu','ぶ');AddJIME('be','べ');AddJIME('bo','ぼ');
AddJIME('pa','ぱ');AddJIME('pi','ぴ');AddJIME('pu','ぷ');AddJIME('pe','ぺ');AddJIME('po','ぽ');
AddJIME('ma','ま');AddJIME('mi','み');AddJIME('mu','む');AddJIME('me','め');AddJIME('mo','も');
AddJIME('ya','や');AddJIME('yu','ゆ');AddJIME('yo','よ');
AddJIME('ra','ら');AddJIME('ri','り');AddJIME('ru','る');AddJIME('re','れ');AddJIME('ro','ろ');
AddJIME('n ','ん');AddJIME('a','あ');AddJIME('i','い');AddJIME('u','う');AddJIME('e','え');AddJIME('o','お');
AddJIME('XO','ヲ');AddJIME('XTS','ッ');AddJIME('XWA','ヮ');
AddJIME('XYA','ャ');AddJIME('XYU','ュ');AddJIME('XYO','ョ');
AddJIME('XA','ァ');AddJIME('XI','ィ');AddJIME('XU','ゥ');AddJIME('XE','ェ');AddJIME('XO','ォ');
AddJIME('KA','カ');AddJIME('KI','キ');AddJIME('KU','ク');AddJIME('KE','ケ');AddJIME('KO','コ');
AddJIME('GA','ガ');AddJIME('GI','ギ');AddJIME('GU','グ');AddJIME('GE','ゲ');AddJIME('GO','ゴ');
AddJIME('SA','サ');AddJIME('SI','シ');AddJIME('SU','ス');AddJIME('SE','セ');AddJIME('SO','ソ');
AddJIME('ZA','ザ');AddJIME('ZI','ジ');AddJIME('ZU','ズ');AddJIME('ZE','ゼ');AddJIME('ZO','ゾ');
AddJIME('TA','タ');AddJIME('TI','チ');AddJIME('TS','ツ');AddJIME('TE','テ');AddJIME('TO','ト');
AddJIME('DA','ダ');AddJIME('ZI','ヂ');AddJIME('ZU','ヅ');AddJIME('DE','デ');AddJIME('DO','ド');
AddJIME('NA','ナ');AddJIME('NI','ニ');AddJIME('NU','ヌ');AddJIME('NE','ネ');AddJIME('NO','ノ');
AddJIME('HA','ハ');AddJIME('HI','ヒ');AddJIME('HU','フ');AddJIME('HE','ヘ');AddJIME('HO','ホ');
AddJIME('BA','バ');AddJIME('BI','ビ');AddJIME('BU','ブ');AddJIME('BE','ベ');AddJIME('BO','ボ');
AddJIME('PA','パ');AddJIME('PI','ぺ');AddJIME('PU','プ');AddJIME('PE','ペ');AddJIME('PO','ポ');
AddJIME('MA','マ');AddJIME('MI','ミ');AddJIME('MU','ム');AddJIME('ME','メ');AddJIME('MO','モ');
AddJIME('YA','ヤ');AddJIME('YU','ユ');AddJIME('YO','ヨ');AddJIME('WA','ワ');
AddJIME('RA','ラ');AddJIME('RI','リ');AddJIME('RU','ル');AddJIME('RE','レ');AddJIME('RO','ロ');
AddJIME('N ','ン');AddJIME('A','ア');AddJIME('I','イ');AddJIME('U','ウ');AddJIME('V','ヴ');AddJIME('E','エ');AddJIME('O','オ');

function AddJIME(H, V) {
    aJIME[aJIME.length] = V;
    bJIME[bJIME.length] = new RegExp(H+'$');
}

function JInput(T) {
    TT = T;
    for (I=0;I<aJIME.length;I++) TT = TT.replace(bJIME[I], aJIME[I]);
    return TT;
}

function JIME(O) {
    if (jinp) O.value=JInput(O.value);
}

function SwitchJI() {
    if (O = document.getElementById('JI')) {
        jinp = 1-jinp;
        O.src = IHOST + 'icon-bg-i'+(jinp+9)+'.gif';
        if (jobj) setTimeout('jobj.focus()',300);
    }
}

function InputNum(E) {
    E = E ? E : event;
    C = E.keyCode ? E.keyCode : E.charCode;
    CC = String.fromCharCode(C);
    if (CC < '0' || CC > '9') {
        E.returnValue = false;
        V = PGjmp.value;
        V = V > PGmax ? PGmax : V;
        if (C == 13 && V >= 1 && V != PGcur) {
            VV = (V-1)*PGsiz;
            H = location.href;
            location.href = H.replace(/(&p=[^&]+)/, '') + '&p=' + VV;
        }
        if (CC >= ' ' && CC <= '~') return false;
    }
}

function NoHot(E) {
    E = E ? E : event;
    E.cancelBubble = true;
}

function InitPG(A, C, Z) {
    PGmax = A;
    PGcur = C;
    PGsiz = Z;
    if (p = document.getElementsByName('pg')) for (P=0;P<p.length;P++) SetPGKeys(p[P]);
}

function SetPGKeys(p) {
    for (I=0;I<p.childNodes.length;I++) {
        O=p.childNodes[I];
        if (O.title) {
            if (O.href) {
                RegKey(O.accessKey, 'location.href="'+O.href+'"');
            } else {
                PGjmp = O;
                O.onkeypress = InputNum;
                RegKey(O.accessKey, 'PGjmp.focus()');
            }
        }
    }
}

function HotKey(E) {
    E = E ? E : event;
    C = E.keyCode ? E.keyCode : E.charCode;
    CC = String.fromCharCode(C).toLowerCase();
    for (I=0;I<aKeys.length;I++) {
        if (CC == aKeys[I]) {
            eval(aActs[I]);
            E.returnValue = false;
            break;
        }
    }
}

function FocusScroll() {
    if (s = document.getElementById('fcs')) s.focus();
}

function AddEvent(E, F, O) {
    O = O ? O : document;
    if (document.addEventListener) O.addEventListener(E, F, false);
    else O.attachEvent('on'+E, F);
}

function InitHotkey() {
    AddEvent('keypress', HotKey);
}

function IW() {
    if (IH) {
        InitWidth();
        setTimeout('IW()', 300);
    }
}

function InitWidth() {
    W = document.body.childNodes[0].offsetWidth;
    if (! ABSC) ABSC = document.getElementById('absscroll');
    if (ABSC) ABSC.style.width = (W - (document.all ? 147 : 154)) + 'px';
}

function RegKey(K, A) {
    aKeys[aKeys.length] = K;
    aActs[aActs.length] = A;
}

function InitReg() {
    InitCookie();
    RegSet('i');
    RegSet('p');
    document.getElementById('leftmenu').onkeypress = NoHot;
    setInterval("LoadONL();", 60000);
}

function RegSet(W) {
    O = document.getElementById(W);
    if (typeof(aCoos[W]) != 'undefined') O.value = aCoos[W];
    return O;
}

function SetLogin(F) {
    LCookie('i',F.elements['i'].value, F.elements['pb'].checked);
    LCookie('p',F.elements['p'].value, F.elements['pb'].checked);
    document.cookie = 'l=1';
    if (ONLT) LoadONL();
    return ONLT == 0;
}

function LoadONL() {
    if (document.getElementById('ONL')) loadjs('?u=N', 1);
}

function ClearLoginUI(I) {
    I.form.elements['i'].value = '';
    I.form.elements['p'].value = '';
    if (O = document.getElementById('setup')) O.innerHTML = '';
    if (O = document.getElementById('MSGN')) O.innerHTML = '';
}

function ClearLogin(I) {
    ClearLoginUI(I);
    SetLogin(I.form);
    aCoos = new Array();
    InitCookie();
}

function ToCb(T) {
    if (top.document.all) top.clipboardData.setData("Text", T);
}

function CE(E) {
    if (typeof(E) == 'undefined') {
    event.cancelBubble=true;
    event.returnValue=false;
    } else E.cancelBubble=true;
}

function loadjs(F, V) {
    nsc = document.createElement("script");
    nsc.type = "text/javascript";
    nsc.src = F+(V?'&'+Math.random():'');
    jsr.appendChild(nsc);
}

function ONL(I, K, L) {
    jpgp = 0;
    document.cookie='j=0';
    if (O = document.getElementById('ONL')) O.innerHTML = I;
    if (L != '') if (O = document.getElementById('CHN')) O.innerHTML = L;
    MsgBox(K);
}

function MsgBox(K) {
    if ((O = document.getElementById('MSGN'))) {
        if (aCoos['i'] && aCoos['p']) TX = K ? '|<a href="?u=msg" target="_blank"><img src="'+IHOST+'icon-bg-i27.gif" title="你有'+K+'個新訊息" />'+K+'個新訊息</a>' : '|<a href="?u=msg" target="_blank"><img src="'+IHOST+'icon-bg-i28.gif" title="觀看訊息記錄" />訊息記錄</a>';
        else TX = '';

        if (O.innerHTML != TX) {
            O.innerHTML = TX;
            if (K && NOMSGS == 0) {
                Sound('message');
                if (DOSK && (! aCoos['K'] || aCoos['K'] == 0)) {
                    MSG('你的熱訊來囉!', '你有'+K+'個新訊息未收, 請點左上角的訊息圖像以接收訊息', '<a href="#" onclick="this.parentNode.parentNode.removeChild(this.parentNode);DOSK=1;return false">關閉</a><a href="?u=msg" target="_blank" onclick="this.parentNode.parentNode.removeChild(this.parentNode)">現在就收</a><a href="?u=user&d=' + aCoos['i'] + '" target="_blank">訊息設定</a>');
                    SHK();
                    DOSK = 0;
                }
            }
        }
    }
}

function Sound(F) {
    if (! document.all) SNDO = document.embeds[0];
    SNDO.SetVariable('SURL','http://cospho.selfip.org/~zordius/snd/'+F+'.mp3');
    SNDO.SetVariable('SVOL', aCoos['S'] ? 100 - aCoos['S'] * 25 : 100);
    SNDO.TGotoFrame('_root', 2);
    SNDO.Play();
}

function DoUpdate() {
    if (aCoos['i'] && aCoos['p']) LoadONL();
}

function MSG(T, C, L) {
    nms = document.createElement('div');
    nms.id = 'msg';
    nms.className = 'err';
    if (L == 'WELCOME') {
        nms.style.top = '50px';
        nms.style.left = '50px';
        nms.style.backgroundColor = '#E0FFE0';
        nms.style.borderColor = '#A0C0A0';
        L = '';
    } else {
        nms.style.top = document.body.clientHeight / 2 + document.body.scrollTop;
    }
    nms.innerHTML = '<h1>' + T + '</h1>' + C + (L ? L : '<a href="#" onclick="this.parentNode.parentNode.removeChild(this.parentNode);return false">關閉</a>') + '<div class="ft"></div>';
    document.body.appendChild(nms);
}

function SHK(T) {
    TT = T ? T-1 : 20;
    if (TT) {
        document.body.scrollTop += (TT%2 ? 5 : -5);
        setTimeout('SHK('+TT+')', 50);
    }
}

function InitCookie() {
    C = document.cookie.split(/; /);
    for (CC in C) {
        if (C[CC]) {
            W = C[CC].split(/=/);
            aCoos[W[0]] = W[1];
        }
    }
}

aERRM = new Array(
    '-',
    '資料庫啟始錯誤',
    '使用者認證錯誤 - 請確定帳號密碼是否正確',
    '資料庫更新錯誤 - 請確定所有欄位是否填寫正確, 是否漏填',
    '圖檔格式錯誤 - 請確定你所上傳的格式為JPG檔或GIF檔, 檔案大小需大於1K',
    '圖檔容量過大 - 請確定你所上傳的圖檔容量低於200K',
    '圖檔重覆 - 請勿重覆上傳相同的圖檔',
    '重覆張貼 - 請勿重覆張貼相同的文章',
    '儲存錯誤 - 圖檔主機發生錯誤, 請稍後再嘗試上傳',
    '封文錯誤 - 為求對自己言論負責, 只能夠封印一週內發表的文章喔',
    '系統暫停 - 現在圖檔上傳系統暫時關閉中, 請等待公告通知'
)

function ERR(C) {
    I = C ? C : aCoos['E'] * 1;
    if (I) alert('錯誤 ['+I+']:'+String.fromCharCode(10,10)+aERRM[I]);
    document.cookie = 'E=0';
}

function Login(O, K) {
    InitCookie();
    I = O ? O : aCoos['l'] * 1;
    if (aCoos['i'] && aCoos['p']) {
        setTimeout('Welcome();', 1000);
        if (O = document.getElementById('setup')) O.innerHTML = '&nbsp;<a href="?u=user&d='+aCoos['i']+'" target="_blank">設定</a>';
        if (O = document.getElementById('logbtn')) O.className = 'logon';
    }
    if (top.MsgBox) MsgBox(K);
    if (I == 2) {
        alert('你已經成功登入!');
        document.cookie = 'l=0';
    }
}

function Welcome() {
    if (! aCoos['w']) {
        MSG('歡迎你的到來!!', '歡迎你回來!!登入向上之後有哪些事可作呢? 你可以: 1.到<b><a href="?u=user&d='+aCoos['i']+'" target="_blank">設定</a></b>去上傳你最近的照片並<b><a href="?u=uicon&d='+aCoos['i']+'" target="_blank">整理相簿</a></b>，或<b><a href="?u=cos&d='+aCoos['i']+'" target="_blank">更新你的角色歷</a></b>，或者<b><a href="?u=book&d='+aCoos['i']+'" target="_blank">登錄你最近的天空相簿</a></b>讓更多人來參觀 2.在<b><a href="?u=L" target="_blank">線上</a></b>找人聊天或者送給<b><a href="?u=birth" target="_blank">壽星</a></b>祝福的話</b>', 'WELCOME');
        aCoos['w'] = 1;
        document.cookie = 'w=1';
    }
}

function SetLogout(I) {
    ClearLoginUI(I);
    document.cookie = 'l=3';
    if (ONLT) LoadONL();
}

function Logout(O) {
    if (O == 4) {
        alert('你已經成功登出!');
        document.cookie = 'l=0';
        document.cookie = 'i=';
        document.cookie = 'p=';
        aCoos['i'] = '';
        aCoos['p'] = '';
        if (O = document.getElementById('logbtn')) O.className = 'logoff';
    }
}

function CheckLogin() {
    if (aCoos['i'] && aCoos['p']) return true;
    else {
        alert('本功能僅限會員使用，請先登入');
        if (I = document.getElementById('i')) I.focus();
        return false;
    }
}

function CheckPost(O) {
    err = '';
    if (O.elements['Content'].value == '') err = '請填寫文章內容';
    if (O.elements['Subj'].value == '') err = '請填寫標題';
    if (! aCoos['i'] || ! aCoos['p']) err = '張貼文章功能僅限會員使用，請先登入';
    if (err) {
        alert(err);
        if (! aCoos['i'] || ! aCoos['p']) document.getElementById('i').focus();
        return false;
    }
    if (O.elements['Forum'].value == 'test') return confirm('請問你確定是要發到『測試練習區』嗎? \n如果你是忘了選討論區，請按取消後重新選擇適當的討論區。\n如果確定是要發測試用文章，請按確定。');
    return true;
}

function InitRotateUIRoom() {
    SetRotate();
    UIR = document.getElementById('uiroom');
    if (UIR && UIR.childNodes.length > 10 && aCoos['r'] != 1) {
        UIT = 0;
        nu = document.createElement("div");
        nu.style.cssText = 'background-color: transparent;border:0';
        RotateUIRoom();
    }
}

function InsertRotate(img) {
    ni = document.createElement("div");
    ni.title = '這是你剛剛成功上傳的照片';
    ico = img.replace(/_o\..../, '_s.jpg');
    ni.className = 'uploaded';
    ni.innerHTML = '<a href="'+ img +'" target="_blank"><img src="'+ico+'" /></a><em>上傳完成!</em><b>你可再繼續上傳</b><a href="?u=mupload">試試大量上傳工具?</a>';
    UIR.appendChild(ni);
    if (UIT > 10 && UIT <30) {
        if (document.all) N.style.filter = '';
        else N.style.MozOpacity = 1;
    }
    if (UIT > 10) UIT = 0;
}

function RotateUIRoom() {
    if (UIT == 0) {
        nu.style.width = '0px';
        UIR.insertBefore(nu, UIR.childNodes[0]);
    }

    if (UIT < 10) nu.style.width = UIT * 15+'px';

    if (UIT == 10) {
        N = UIR.childNodes[UIR.childNodes.length - 1];
        if (document.all) N.style.filter = 'alpha(opacity=0,style=0)';
        else N.style.MozOpacity = 0;
        UIR.replaceChild(N, nu);
    }

    if (UIT > 10) {
        if (UIT >= 30) {
            if (document.all) N.style.filter = '';
            else N.style.MozOpacity = 1;
        } else {
            if (document.all) N.style.filter = 'alpha(opacity='+((UIT-10) * 5)+',style=0)';
            else N.style.MozOpacity = (UIT - 10) / 20;
        }
    }

    UIT = (++UIT % 50);

    if (aCoos['r'] != 1) setTimeout('RotateUIRoom()', 150);
}

function SetRotate(O) {
    if (O) {
        aCoos['r'] = O.checked?1:0;
        LCookie('r', aCoos['r']);
    } // else document.getElementById('rochk').checked = (aCoos['r'] == 1);
}

function CountDown(X) {
    if (O = document.getElementById('counter')) {
        O.innerHTML = X;
        if (X) setTimeout('CountDown('+(X-1)+')', 1000);
        else O.parentNode.style.display = 'none';
    }
}

function MyIP(X) {
    if (O = document.getElementById(X)) O.style.border = '1px solid #FF0000';
}

function LCookie(A, B, N) {
    document.cookie = A+'='+B+(N?'':';expires=Fri, 2 Jan 2099 00:00:00 GMT;');
}

function CheckUJPG(O, R) {
    if (! CheckLogin()) return false;

    err = '';
    if (O.elements['UJ'].value == aCoos['jn']) err = '請勿上傳相同的檔案';
    if (! R && ! O.elements['UD'].value) err = '請填寫角色與作品名稱';
    if (! O.elements['UJ'].value) err = '請選擇你要上傳的檔案';

    if (err) {
        alert(err);
        return false;
    }
    O.elements['I'].value = aCoos['i'];
    O.elements['P'].value = aCoos['p'];
    document.cookie = 'jn=' + O.elements['UJ'].value;
    if (self.frames['upjpg']) {
        O.target = 'upjpg';
        O.elements['JPGP'].value = '上傳中, 請稍候...';
        O.elements['JPGP'].disabled = true;
    }
    return true;
}

function InitUsered(U, X) {
    X = X ? 0 : (aCoos['p'] && (aCoos['i'] == U));
    U = document.getElementsByTagName('form');
    for (I=0;I<U.length;I++) {
        U[I].id = X ? 'usered' : 'user';
        U[I].className = U[I].className;
    }
}

function PreviewUser() {
    InitUsered('', 1);
    alert('按下F5可以結束預覽');
}

function DSCatchOnDown() {
    if (eventx < document.body.offsetWidth - 20) {
        dragstart = 1;
        seld.SX = eventx;
        seld.EX = eventx;
        seld.SY = eventy;
        seld.EY = eventx;
    }
}

function DSCatchOnUp() {
    if (dragstart) {
        seld.style.width = 0;
        seld.style.height = 0;
        seld.style.left = -10;
        seld.style.top = -10;
        IO = seld.O.getElementsByTagName('input');
        if (Math.abs(seld.SX-seld.EX) + Math.abs(seld.SY+seld.EY) > 40) {
            X = Math.min(seld.SX, seld.EX);
            Y = Math.min(seld.SY, seld.EY);
            XX = Math.max(seld.SX, seld.EX);
            YY = Math.max(seld.SY, seld.EY);
            for (I=0;I<IO.length;I++) if (IO[I].name == 'sel') {
                OX = 0;
                OY = 0;
                O = IO[I];
                while (O != document.body) {
                    OX += O.offsetLeft;
                    OY += O.offsetTop;
                    O = O.offsetParent;
                }
                if (OX >= X && OX < XX && OY >= Y && OY < YY) IO[I].checked = 1;
            }
        }
        dragstart = 0;
        seld.O.style.cursor = '';
    }
}

function DSCatchOnMove(E) {
    E = E ? E : event;
    eventx = E.clientX + document.body.scrollLeft - 2;
    eventy = E.clientY + document.body.scrollTop - 2;
    if (dragstart) {
        C = E.button || E.which;
        if (! (C & 1)) {
            DSCatchOnUp;
            return;
        }
        seld.EX = eventx;
        seld.EY = eventy;
        if (Math.abs(seld.SX-seld.EX) + Math.abs(seld.SY+seld.EY) > 40) {
            seld.O.style.cursor = 'crosshair';
            seld.style.left = Math.min(seld.SX, seld.EX);
            seld.style.top = Math.min(seld.SY, seld.EY);
            seld.style.width = Math.abs(seld.SX - seld.EX);
            seld.style.height = Math.abs(seld.SY - seld.EY);
        }
    }
}

function DSCatchOnSelect(E) {
    return false;
}

function InstallDragSelect(ID) {
    if (seld = document.getElementById('SELD')) {
        var O = document.getElementById(ID);
        seld.O = O;
        AddEvent('mousemove', DSCatchOnMove);
        AddEvent('mousedown', DSCatchOnDown);
        AddEvent('mouseup', DSCatchOnUp);
        AddEvent('selectstart', DSCatchOnSelect);
    } else setTimeout('InstallDragSelect("'+ID+'")', 100);
}

window.onresize = InitWidth;
jsloadok = 1;